﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using UsageTracker.Reporting.Data;

namespace UsageTracker.Reporting.Pages
{
    public partial class ProdInstancesMap : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            RenderMap("SAMPLE-APP");
        }


        /// <summary>
        /// 
        /// </summary>
        private void RenderMap(string productName)
        {
            UsageDataAccess DAL = new UsageDataAccess();
            List<List<string>> geoDetails = DAL.GetAllHostsForProduct(productName, DateTime.MinValue, DateTime.MaxValue);
            int markCounter = 1;
            string mapJs = String.Empty;
            mapJs += "<script>";
            mapJs += "function initialize() {";
            mapJs += "var mapProp = {center: new google.maps.LatLng(39, 70.00),zoom: 2,mapTypeId: google.maps.MapTypeId.ROADMAP};";
            mapJs += "var map = new google.maps.Map(document.getElementById('map_canvas'), mapProp);";

            geoDetails.ForEach(
                m =>
                {
                    string country = m[0];
                    string city = m[1];
                    string lat = m[2];
                    string lon = m[3];
                    string host = m[4];
                    mapJs += "var marker" + markCounter + " = new google.maps.Marker({position: new google.maps.LatLng(" + lat + "," + lon + "),map: map,title: '" + host + " @ " + city + ", " + country + "'});";
                    mapJs += "marker" + markCounter + ".setMap(map);";
                    markCounter++;
                });

            mapJs += "}";
            mapJs += "google.maps.event.addDomListener(window, 'load', initialize);";
            mapJs += "</script> ";
            this.MapJs.Text = mapJs;
        }
    }
}